app.controller('RegionIpHddCtrl', ['$scope','$state','$uibModal', '$stateParams','$http','DiagramService','$anchorScroll','$location','$filter','DataService',RegionIpHddCtrl]);

function RegionIpHddCtrl($scope,$state,$uibModal, $stateParams,$http,DiagramService,$anchorScroll,$location,$filter,DataService){
	$anchorScroll.yOffset = -40; 
	$location.hash();
	$anchorScroll();

    $scope.hintOption = {
        show:false,
        hinttitle:'',
        hintinfo:'111',
        typeImg:'../img/对号.png',
        btn:'继续添加',
        cancelBtn:'返回首页'
    }


    $scope.SAndCGroup = []

    $scope.changeFlag = ''

    $scope.csvarea = ''

    $scope.freshCSV = function(){
        $scope.csvarea = ''
    }

    $scope.submitCSV = function(){
        $scope.newCSV = []
        var csvGroup = $scope.csvarea.split("\n")
        for(var i=0;i<csvGroup.length;i++){
            var csv = csvGroup[i].split(",")
            if(csv.length>1){
                var record = {
                    ajbh:csv[0],
                    time:csv[1],
                    afdd:csv[2],
                    street:csv[4],
                    community:csv[6],
                    type:csv[5],
                    bjnr:csv[3],
                    repeat:1,
                    jd:'',
                    wd:''
                }
                $scope.newCSV.push(record)
            }
        }
        var modalInstance = $uibModal.open({
            animation: $scope.animationsEnabled,
            templateUrl: '../templates/region/region-import-hdd-table.html',
            controller: 'RegionIpHddTbCtrl',
            controllerAs: 'vm',
            backdrop: "static",
            size: "lg",
            resolve: {
                newCSV: function () {
                    return $scope.newCSV;
                }
            }
        })
        modalInstance.result.then(function(result) {  
            getList();
        });  
    }

    $scope.fresh = function(){
        $scope.form={
            ajbh:'',
            time:'',
            afdd:'',
            street:'',
            community:'',
            type:'',
            bjnr:'',
            repeat:1,
            jd:'',
            wd:''
        }
        $scope.changeFlag = ''
    }
    $scope.fresh();

    function getCommunity(){
        DataService.restGetApi('communities?filter={"where":{"isdelete":0}}')
        .then(function success (data) {
            $scope.SAndCGroup = data;
            })
    }

    getCommunity();

    $scope.typeGroup=['黄','赌','毒']

    $scope.repeatGroup = [1,2,3,4,5]

    $scope.dateOptions = {
        showOn:'both',
        buttonImage:'../img/calender.png',
	    changeYear: true,
	    changeMonth: true,
        dateFormat:'yy-mm-dd'
    };

    $scope.pushStreet = function(item){
        if(item!=$scope.form.street){
            $scope.form.street = item;
        }
    }

    $scope.checkCommunity = function(item){
        if($scope.form.community==''||$scope.form.community.street!=item.street){
            $scope.form.community = item;
        }
    }

    $scope.submitForm = function(){
        $scope.importForm.ajbh.$dirty = true;
        $scope.importForm.afdd.$dirty = true;
        $scope.importForm.street.$dirty = true;
        $scope.importForm.community.$dirty = true;
        $scope.importForm.type.$dirty = true;
        $scope.importForm.bjnr.$dirty = true;
        if($scope.importForm.ajbh.$valid&&$scope.importForm.afdd.$valid&&$scope.importForm.street.$valid&&$scope.importForm.community.$valid&&$scope.importForm.type.$valid&&$scope.importForm.bjnr.$valid){
            $scope.form.street = $scope.form.street.street;
            $scope.form.community = $scope.form.community.community;
            $scope.form.time = $filter('date')($scope.form.time, 'yyyy-MM-dd')
            DataService.restPostApi('pornographies/dateChangeByAjbh',$scope.form)
            .then(function success (data) {
                $scope.hintOption = {
                    show:true,
                    hinttitle:data.top,
                    hintinfo:'',
                    typeImg:'../img/对号.png',
                    btn:'继续添加',
                    cancelBtn:'返回首页'
                }
                $scope.clickCancel = function(){
                    $state.go("app.region.main")
                    $scope.hintOption.show = false;
                }

                $scope.clickSubmit = function(){
                    $scope.fresh();
                    getList();
                    $scope.importForm.ajbh.$dirty = false;
                    $scope.importForm.afdd.$dirty = false;
                    $scope.importForm.street.$dirty = false;
                    $scope.importForm.community.$dirty = false;
                    $scope.importForm.type.$dirty = false;
                    $scope.importForm.bjnr.$dirty = false;
                    $scope.changeFlag = ''
                    $scope.hintOption.show=false;
                }

            })
        }
    }

    $scope.searchText = ''

    $scope.sortText = 'ajbh'

    $scope.$watch('searchText',function(newValue,oldValue){
        if(newValue != oldValue){
            var filtList = $filter('filter')(list3,newValue)
            list = $filter('orderBy')(filtList,$scope.sortText)
            list2 = deepCopy(list)
            $scope.pageOption['current'] = 0;
            $scope.getData([1,$scope.pageOption.pageSize])
        }
    })

    $scope.$watch('sortText',function(newValue,oldValue){
        if(newValue != oldValue){
            var filtList = $filter('filter')(list3,$scope.searchText)
            list = $filter('orderBy')(filtList,newValue)
            list2 = deepCopy(list)
            $scope.pageOption['current'] = 0;
            $scope.getData([1,$scope.pageOption.pageSize])
        }
    })

    $scope.clickSort = function(type){
        console.log(type)
        $scope.sortText = type;
    }

    $scope.importType = 'single'


    $scope.changeImportType = function(type){
        $scope.importType = type;
    }

    $scope.clearInput = function(input){
        $scope.form[input] = ''
    }

    $scope.pageOption = {
	    pageSize:10,
	    itemCount:3 
  	}

    var list = [];
    var list2 = [];
    var list3 = []
	
	$scope.getData = function(item){
		if(item) {
			generateData(item);
		}
	}

    function getList(){
        DataService.restGetApi('pornographies')
        .then(function success (data) {
            list = data;
            list2 = deepCopy(list)
            list3 = deepCopy(list)
            $scope.getData([1,$scope.pageOption.pageSize])
        })
    }
    getList();

	function generateData(item) {
		$scope.pageOption.itemCount = list.length;
		$scope.commList = list.slice(item[0]-1,item[1]);
	}

    $scope.change = function(index){
        $anchorScroll();
        $scope.changeFlag = 'change'
        $scope.form= list2[index]
        $scope.form.street = {community:list2[index].community,street:list2[index].street};
        $scope.form.community = {community:list2[index].community,street:list2[index].street};
    }

    $scope.delete = function(ajbh){
        if(confirm('确认删除么？')){
            DataService.restPostApi('pornographies/deleteByAjbh',{ajbh:ajbh})
            .then(function success (data) {
                $scope.hintOption = {
                    show:true,
                    hinttitle:data.top,
                    hintinfo:'',
                    typeImg:'../img/对号.png',
                    btn:'继续操作',
                    cancelBtn:'返回首页'
                }
                $scope.clickCancel = function(){
                    $state.go("app.region.main")
                    $scope.hintOption.show = false;
                }

                $scope.clickSubmit = function(){
                    getList();
                    $scope.hintOption.show = false;
                }
            })
        }    
    }

}
